/**
 * PyCompanyQueryReportClientImpl.java
 * author: yujiakui
 * 2017年8月28日
 * 下午4:53:19
 */
package com.ctim.service.integration.pengyuan.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.ctim.model.ThirdPartyInvokeCfg;
import com.ctim.service.enums.ThirdPartyReturnTypeEnum;
import com.ctim.service.integration.pengyuan.ThirdPartyInvokeClient;
import com.ctim.service.integration.pengyuan.ThirdPartyQueryInvokeService;

/**
 * @author yujiakui
 *
 *         下午4:53:19
 *
 *         第三方报告查询接口
 */
@Component
public class ThirdPartyInvokeClientImpl implements ThirdPartyInvokeClient {

	@Override
	public Object invoke(ThirdPartyInvokeCfg thirdPartyInvokeCfg, String interfaceName,
			String queryCondition, ThirdPartyReturnTypeEnum thirdPartyReturnTypeEnum) {

		// 设置ssl
		setSslCfg(thirdPartyInvokeCfg);

		Object[] queryResult = ThirdPartyQueryInvokeService.invoke(thirdPartyInvokeCfg.getUrl(),
				interfaceName,
				new Object[] { thirdPartyInvokeCfg.getUserId(), thirdPartyInvokeCfg.getPassword(),
						queryCondition, thirdPartyReturnTypeEnum.getCode() });

		return queryResult[0];
	}

	/**
	 * 设置ssl 配置
	 *
	 * @param thirdPartyInvokeCfg
	 */
	private void setSslCfg(ThirdPartyInvokeCfg thirdPartyInvokeCfg) {
		// 设置ssl
		if (StringUtils.isNotEmpty(thirdPartyInvokeCfg.getSslKeyStorePassword())
				&& StringUtils.isNotEmpty(thirdPartyInvokeCfg.getSslKeyStoreUrl())) {
			System.setProperty("javax.net.ssl.keyStore", thirdPartyInvokeCfg.getSslKeyStoreUrl());
			System.setProperty("javax.net.ssl.keyStorePassword",
					thirdPartyInvokeCfg.getSslKeyStorePassword());
		}

	}

}
